#pragma once
#include "GLBackGround.h"
#include "GLBillBoardRender.h"
#include "GLCommon.h"
#include "GLIconRender.h"
#include "GLLineRender.h"
#include "GLRect.h"
#include "common.h"
struct GLRenderComp;
class RenderController : public GL_FUN
{
public:
	// 单例入口
	static RenderController* getInstance();

	~RenderController();

public:
	qreal scale() const
	{
		return mScale;
	}
	void setScale(qreal v, bool update = true);

	QSize backgroundSize() const;

	QSize viewSize() const
	{
		return mViewSize;
	}
	void setViewSize(const QSize& v, bool update = true);

	GLBackground* background()
	{
		return mBackground;
	}
	GLRect* rect()
	{
		return mRect;
	}
	QList<GLRenderComp*> comps()
	{
		return mComps;
	}
	GLIconRender* iconRender()
	{
		return mIconRender;
	}
	GLLineRender* lineRender()
	{
		return mLineRender;
	}
	GLBillBoardRender* billBoardRender()
	{
		return mBillBoardRender;
	}

public:
	void init(const QSize& size);
	void unInit();
	bool updateCount(int v);
	void paint();

	// 界面StartMove功能，当前选中的Billboard 文本随时间刷新
	void controlMoveUpdate(const QString& timeStr);

	// 键盘 W、S、A、D 功能，选中的Billboard、Icon和Line同时移动
	void controlCompTranslate(int tx, int ty);

public:
	void makeCurrent()
	{
		if (mMakeCurrentCall)
		{
			mMakeCurrentCall();
		}
	}

	void doneCurrent()
	{
		if (mDoneCurrentCall)
		{
			mDoneCurrentCall();
		}
	}
	void renderRequest()
	{
		if (mRenderRequestCall)
		{
			mRenderRequestCall();
		}
	}
	void setMakeCurrentCall(const Callback& call)
	{
		mMakeCurrentCall = call;
	}
	void setDoneCurrentCall(const Callback& call)
	{
		mDoneCurrentCall = call;
	}
	void setRenderRequestCall(const Callback& call)
	{
		mRenderRequestCall = call;
	}

private:
	void initShaderProgram();
	void initCompDatas();

private:
	// 单例的关键，构造函数私有，外部不能创建此对象,只能通过入口函数getInstance使用。
	RenderController();

private:
	QOpenGLShaderProgram* mColorProgram			  = nullptr;
	QOpenGLShaderProgram* mTextureProgram		  = nullptr;
	QOpenGLShaderProgram* mTextureInstanceProgram = nullptr;
	QOpenGLShaderProgram* mColorInstanceProgram	  = nullptr;
	GLBackground*		  mBackground			  = nullptr;
	GLRect*				  mRect					  = nullptr;
	QMatrix4x4			  mMVPMat;
	QSize				  mViewSize;
	qreal				  mScale = 0.3;
	uint32_t			  mCount = 0;

	QList<GLRenderComp*> mComps;

	GLIconRender*	   mIconRender		= nullptr;
	GLLineRender*	   mLineRender		= nullptr;
	GLBillBoardRender* mBillBoardRender = nullptr;

	Callback mMakeCurrentCall;
	Callback mDoneCurrentCall;
	Callback mRenderRequestCall;
};
// 简短的宏
#define gController RenderController::getInstance()
